﻿using System;
using System.Collections.Generic;

namespace Vbyte.DataSource.Cache
{
    /// <summary>
    /// 可缓存的实体基类
    /// </summary>
    [Serializable]
    public abstract class CacheableBase : TableEntry, IObjectIdentification<PropertyBindIdentity>, ICacheTimeSupport
    {

        #region IObjectIdentification<PropertyBindIdentity> 成员

        /// <summary>
        /// 获取该对象的标志
        /// </summary>
        /// <returns></returns>
        public abstract PropertyBindIdentity GetIdentity();

        /// <summary>
        /// 设置该对象的标志
        /// </summary>
        /// <param name="identity">标志值存储对象</param>
        public virtual void SetIdentity(PropertyBindIdentity identity)
        {
            if (identity != null)
            {
                identity.BindInstance(this.GetType(), this);
            }
        }

        #endregion

        /// <summary>
        /// 缓存时间
        /// </summary>
        [Comment("缓存时间")]
        [Default("getdate()")]
        [Nullable(false)]
        public DateTime CacheTime { get; set; }

        #region ICacheTimeSupport 成员

        /// <summary>
        /// 获取缓存时间(UTC格式)
        /// </summary>
        /// <returns></returns>
        public DateTime GetCacheTimeUTC()
        {
            return CacheTime.ToUniversalTime();
        }

        /// <summary>
        /// 当前数据的更新时间(UTC格式)
        /// </summary>
        /// <returns></returns>
        public virtual DateTime GetUpdateTimeUTC()
        {
            return GetCacheTimeUTC();
        }

        /// <summary>
        /// 设置缓存的更新时间(UTC格式)
        /// </summary>
        /// <param name="dtUTC">缓存的更新时间(UTC格式)</param>
        public virtual void UpdateCacheTimeUTC(DateTime dtUTC)
        {
            CacheTime = dtUTC.ToLocalTime();
        }
        #endregion
    }
}
